package com.dushibao.service.impl;

import com.dushibao.common.Constants;
import com.dushibao.dao.AddressDao;
import com.dushibao.entity.Address;
import com.dushibao.service.AddressService;
import com.dushibao.utils.Page;
import com.dushibao.utils.SecurityHolder;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * 收货地址 实现类
 *
 * @author zqd
 *
 * @date 2022-06-28 19:50:35
 */
public class AddressServiceImpl implements AddressService {
       AddressDao addressDao = new AddressDao();

       @Override
       public Integer save(Address address) {
            return addressDao.save(address);
       }

       @Override
       public Page page() {
            HttpServletRequest req = SecurityHolder.request.get();
            String current = req.getParameter("current");
            String pageSize = req.getParameter("pageSize");

            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");

            Map<String,Object> params = new HashMap<>();
            params.put("startTime",startTime);
            params.put("endTime",endTime);

            req.setAttribute("startTime",startTime);
            req.setAttribute("endTime",endTime);

            Page page = new Page();
            page.setCurrent(Integer.parseInt(current==null?"1":current));
            page.setPageSize(Integer.parseInt(pageSize==null?"10":pageSize));

            return addressDao.page(page,params);
       }

       @Override
       public Address getById(long id) {
            return addressDao.getById(id);
       }

       @Override
       public Integer saveOrUpdate(Address address) {
            if(address.getId()!=null){
                 return addressDao.update(address);
            }
            return addressDao.save(address);
       }

       @Override
       public Integer deleteById(List<Long> ids) {
            return addressDao.deleteById(ids);
       }
}
